package com.whale.cloud.util.rest.condition;

import com.whale.cloud.exception.CommonException;
import com.whale.cloud.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/** Created by paco on 5/6/2017. */
public enum ERelation {
  greaterthan(">", "大于"),
  greaterthanequalto(">=", "大于等于"),
  lessthan("<", "小于"),
  lessthanequalto(">=", "小于等于"),
  equal("=", "等于"),
  notequal("!=", "不等于"),
  like("LIKE", ""),
  notlike("NOT LIKE", ""),
  exists("EXISTS", ""),
  notexists("NOT EXISTS", ""),
  regexp("REGEXP", ""),
  notregexp("NOT REGEXP", ""),
  in("IN", "包含"),
  notin("NOT IN", "不包含"),
  range(":", "");

  private static Map<String, ERelation> allrelation = new HashMap<String, ERelation>();

  static {
    allrelation.put(">", greaterthan);
    allrelation.put(">=", greaterthanequalto);
    allrelation.put("<", lessthan);
    allrelation.put("<=", lessthanequalto);
    allrelation.put("=", equal);
    allrelation.put("!=", notequal);
    /*allrelation.put("like",like);
    allrelation.put("not like",notlike);
    allrelation.put("exists",exists);
    allrelation.put("not exists",notexists);
    allrelation.put("regexp",regexp);
    allrelation.put("not regexp",notregexp);
    allrelation.put("in",in);
    allrelation.put("not in",notin);*/
    allrelation.put(":", range);
  }

  private String code;
  private String desc;

  ERelation(String code, String desc) {
    this.code = code;
    this.desc = desc;
  }

  public static ERelation getERelation(String code) {
    ERelation er = null;
    if (StringUtils.isNull(code)) {
      throw new CommonException("-40", "关系运算符不能为空");
    }
    String tempCode = code.replaceAll("^\\s+|\\s+$", "");
    tempCode = code.replaceAll("\\s+", " ");
    if (!allrelation.containsKey(tempCode)) {
      throw new CommonException("-40", "关系运算符错误");
    }
    er = allrelation.get(tempCode.toLowerCase());

    return er;
  }

  public String getCode() {
    return this.code;
  }

  public String getDesc() {
    return this.desc;
  }
}
